package com.music.activity;
import java.util.ArrayList;

import com.music.R;
import com.music.adapter.MusicListAdapter;
import com.music.utilbean.DBhelper;
import com.music.utilbean.MediaUtils;
import com.music.utilbean.Mp3Info;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Tab1ActivityMusic extends MusicBaseActivity implements OnItemClickListener{
private ListView           listView;
private ArrayList<Mp3Info> mp3Infos;
private MusicListAdapter   musicListAdapter;
public static String user="还未登陆";
	  @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab1);
		listView=(ListView) findViewById(R.id.lovemusic_list);
		listView.setOnItemClickListener(this);
		helper=new DBhelper(getBaseContext(), "music.db", null, 5);
	    mp3Infos=new ArrayList<Mp3Info>();
		loadSQLData();
		
   
	}
	  
	  
	@Override
	public void publish(int progress) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void change(int position) {
		// TODO Auto-generated method stub
		
	}
	@Override
		protected void onResume() {
			// TODO Auto-generated method stub
			super.onResume();
			bindplayService();
			loadSQLData();
		}
	@Override
		protected void onPause() {
			// TODO Auto-generated method stub
			super.onPause();
			unbindplayService();
		}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		playService.play((int)mp3Infos.get(arg2).getMp3infoID());
		Intent intent=new Intent(Tab1ActivityMusic.this,PlayActivityMusic.class);
		//intent.putExtra("position", arg2);
		startActivity(intent);
		
	}
	private void loadSQLData() {		
	mp3Infos.clear();
	SQLiteDatabase db=	helper.getWritableDatabase();
	
	String[]columns=new String[]{"mp3infoID"};
	
 Cursor cursor= db.query("mymusic", columns,null,null,null,null,null,null);
Mp3Info mp3Info;
 if(cursor!=null){
		 while(cursor.moveToNext()){
		int	 mp3infoID= Integer.parseInt(cursor.getString(cursor.getColumnIndex("mp3infoID")));
		 mp3Info=MediaUtils.getMp3Infos(this).get(mp3infoID);
		 mp3Info.setMp3infoID(mp3infoID);
	     mp3Infos.add(mp3Info);
		 } 
	    musicListAdapter=new MusicListAdapter(this,mp3Infos);
		listView.setAdapter(musicListAdapter);
		
 }
	}
	
//添加登陆注册菜单	    
@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.loginmenu, menu);
	return true;
	}
@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	
    switch (item.getItemId()) {
    case R.id.login_item:
    	//创建弹框
    	LayoutInflater factory=LayoutInflater.from(Tab1ActivityMusic.this);
	AlertDialog.Builder loginbuilder=new AlertDialog.Builder(Tab1ActivityMusic.this);
	loginbuilder.setTitle("用户登陆");
	final View loginView = factory.inflate(R.layout.login_view, null);
		loginbuilder.setView(loginView);
		loginbuilder.setPositiveButton("登录", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				
				EditText username=(EditText)loginView.findViewById(R.id.username);
				EditText password=(EditText)loginView.findViewById(R.id.password);
				String uname=username.getText().toString();
				String psw=password.getText().toString();
				SQLiteDatabase db=	helper.getWritableDatabase();
				Cursor cursor=db.rawQuery("select * from user where username=? and password=?",new String[]{uname,psw});
			boolean flag=cursor.moveToNext();
			if(flag){
			 Toast.makeText(getApplicationContext(),
					     "登陆成功", Toast.LENGTH_LONG).show();
			user=uname;
			}else {
				 Toast.makeText(getApplicationContext(),
					     "登陆失败", Toast.LENGTH_LONG).show();
			}
				
			}
		});	
		loginbuilder.show();
		
        break;
    case R.id.regist_item:                        
    	//创建弹框
    	LayoutInflater factory2=LayoutInflater.from(Tab1ActivityMusic.this);
	AlertDialog.Builder registbuilder=new AlertDialog.Builder(Tab1ActivityMusic.this);
	registbuilder.setTitle("用户注册");
	final View registView = factory2.inflate(R.layout.regist_view, null);
		registbuilder.setView(registView);
		registbuilder.setPositiveButton("注册", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
	
				EditText newusername=(EditText)registView.findViewById(R.id.newusername);
				EditText newpassword=(EditText)registView.findViewById(R.id.newpassword);
				String newuname=newusername.getText().toString();
				String newpsw=newpassword.getText().toString();
				SQLiteDatabase db=	helper.getWritableDatabase();
				ContentValues values=new ContentValues();
				values.put("username", newuname);
				values.put("password", newpsw);
				db.insert("user", null, values);
				Toast.makeText(getApplicationContext(),
					     "注册成功", Toast.LENGTH_LONG).show();
			}
			});	
		registbuilder.show();
        break;
    default:
}                                       
return true;

}
}

